import axios from 'axios';

// 创建axios实例
const $axiso = axios.create({
  baseURL: 'http://192.168.124.9:8888', 
  timeout: 5000
});

// 请求拦截器
$axiso.interceptors.request.use(
  config => {
    // 配置token
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 响应拦截器
$axiso.interceptors.response.use(
  response => {
    // 如果是 blob 类型，直接返回整个 response
    if (response.config.responseType === 'blob') {
      return response;
    }
    return response.data;
  },
  error => {
    // 统一处理错误信息
    if (error.response) {
      const msg = error.response.data?.message || error.response.statusText || '请求错误';
      // 可用Element UI等弹窗组件提示
      if (window && window.Element && window.Element.Message) {
        window.Element.Message.error(msg);
      } else if (window && window.$message) {
        window.$message.error(msg);
      } else {
        // 兜底
        alert(msg);
      }
    } else {
      alert('网络异常或服务器无响应');
    }
    return Promise.reject(error);
  }
);

export default $axiso;

